package com.tgy.algorithm.base._暴力递归;

import java.util.Stack;

/**
 * 在不开辟新空间的情况下，借助递归把一个栈倒序
 */
public class _通过递归把一个栈倒序不借助其他空间 {


    /**
     * 获取栈底元素，其余元素位置不变
     * @param stack
     * @return
     */
    public static int getStackBottom(Stack<Integer> stack) {

        Integer value = stack.pop();
        if (stack.isEmpty()) {
            return value;
        }
        int bottomVal = getStackBottom(stack);
        stack.push(value);
        return bottomVal;
    }

    public static void reverseStack(Stack<Integer> stack) {

        if (stack.isEmpty()) {
            return;
        }
        int stackBottom = getStackBottom(stack);
        reverseStack(stack);
        stack.push(stackBottom);
    }


    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        /**
         * 3
         * 2
         * 1
         */
        stack.push(1);
        stack.push(2);
        stack.push(3);

//        int stackBottom = getStackBottom(stack);
        /**
         * 2
         * 1
         */
//        System.out.println(stackBottom);
//        System.out.println(stack);
//
//        while (!stack.isEmpty()) {
//            System.out.println(stack.pop());
//        }
        reverseStack(stack);
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
